import re

def trim(elm):
  """
  Perform a trim on a list.
  """
  return [x for x in elm if len(x.strip()) > 0]

def cssSelector(string):
    """
    Return the selector from a line of css
    """
    syntax = re.compile(r'(.*?) {.*?}', re.S)
    
    return syntax.sub('\\1', string).strip()

def cssRule(string):
    """
    Return the rule from a line of css
    """
    syntax = re.compile(r'.*? {(.*?)}', re.S)
    
    return syntax.sub('\\1', string)

def byCssLine(string):
    """Check given source for CSS"""
    
    syntax = re.compile(r'(.*? {.*?})', re.S)
    
    return syntax.split(string)

def loose_in(where, what):
    if what in where:
        return True
    
    for x in where:
        if what in x:
            return True
    
    return False

def flatten(x):
    # Credits to 
    # http://kogs-www.informatik.uni-hamburg.de/~meine/python_tricks
    """flatten(sequence) -> list

    Returns a single, flat list which contains all elements retrieved
    from the sequence and all recursively contained sub-sequences
    (iterables)."""

    result = []
    for el in x:
        if isinstance(el, list):
            result.extend(flatten(el))
        else:
            result.append(el)
    return result

def returnreversed(l):
    """
    Return a reversed copy of the given list
    """
    if not isinstance(l, list):
        raise AttributeError, type(l)

    _ = list(l) # Copying

    _.reverse()

    return _